﻿namespace Catalog.API.Products.GetProducts
{
    using Catalog.API.Models;
    using Marten.Pagination;
    using System.Threading;
    using System.Threading.Tasks;

    //注意理解record和class的区别，括号内是record的属性
    //实际作用是起到dto的作用,用户返回给前端的数据
    public record GetProductsResult(IEnumerable<Product> Products);

    //实际作用是起到dto的作用,用户发起http请求时，作为body发送的json数据
    public record GetProductsQuery(int PageNumber, int PageSize) : IQuery<GetProductsResult>;
    public class GetProductsQueryHandler(IDocumentSession session) : IQueryHandler<GetProductsQuery, GetProductsResult>
    {
        public async Task<GetProductsResult> Handle(GetProductsQuery query, CancellationToken cancellationToken)
        {
            var products = await session.Query<Product>().ToPagedListAsync(query.PageNumber, query.PageSize,  cancellationToken);
           
            return new GetProductsResult(products);
        }
    }
}
